
 $(function() {
	openDB();
	selectNewData();
 });
function createtable(){
	
	var createsql = 'create table if not exists ybrq('
	          +'"personid" char(100),'
			  +'"name" char(100),'
			  +'"sex" char(100),'
			  +'"happentime" char(100),'
			  +'"visityType" char(20),'
			  +'"tw" char(20),'
			  +'"ml" char(200),'
			  +'"hxpl" char(200),'
			  +'"gy" char(200),'
			  +'"dy" char(200),'
			  +'"height" char(200),'
			  +'"weight" char(200),'
			  +'"yaowei" char(200),'
			  +'"bmi" char(200),'
			  +'"ysxg" char(50),'
			  +'"xyqk" char(20),'
			  +'"rxyl" char(20),'
			  +'"yjpl" char(20),'
			  +'"ryjl" char(20),'
			  +'"sfjj" char(20),'
			  +'"jjnl" char(20),'
			  +'"zybjcs" char(20),'
			  +'"gz" char(200),'
			  +'"cysj" char(50),'
			  +'"fc" char(20),'
			  +'"fcfhcs" char(200),'
			  +'"fcfhcsc" char(200),'
			  +'"fswz" char(20),'
			  +'"fswzfhcs" char(200),'
			  +'"fswzfhcsc" char(200),'	
			  +'"wlys" char(20),'
			  +'"wlysfhcs" char(200),'
			  +'"wlysfhcsc" char(200),'
			  +'"hxwz" char(20),'
			  +'"hxwzfhcs" char(200),'
			  +'"hxwzfhcsc" char(200),'
			  +'"qtdw" char(20),'
			  +'"qtdwfhcs" char(200),'
			  +'"qtdwfhcsc" char(200),'
			  +'"wxyskz" char(20),'
			  +'"mbtz" char(20),'
			  +'"wxyskzqt" char(100),'	 			 
			  +'"sfys" char(20)'
	          +')' 
			 
	executeSQL(createsql);//创建表单
}  
function saveInfo(){
	//createtable();
	var happentime = $("#happentime").val();//随访日期
	var visityType = $("input[name='sffs']:checked").val();//随访方式
	var tw = $("#tw").val();//体温
	var ml = $("#ml").val();//脉率
	var hxpl = $("#hxpl").val();//呼吸频率
	var gy = $("#gy").val();//高压
	var dy = $("#dy").val();//低压
	var height = $("#height").val();//身高
	var weight = $("#weight").val();//体重
	var yaowei = $("#yaowei").val();//腰围
	var bmi = $("#bmi").val();//bmi
	var ysxg = $("input[name='ysxg']:checked").val();//饮食习惯
	var xyqk = $("input[name='xyqk']:checked").val();//吸烟情况
	var rxyl = $("#rxyl").val();//日吸烟量
	var yjpl = $("input[name='yjpl']:checked").val();//饮酒频率
	var ryjl = $("#ryjl").val();//日饮酒量
	var sfjj = $("input[name='sfjj']:checked").val();//是否戒酒
	var jjnl = $("#jjnl").val();//戒酒年龄
	var zybjcs = $("input[name='zybjcs']:checked").val();//职业病危害因素接触史
	var gz = $("#gz").val();//工种
	var cysj = $("#cysj").val();//从业时间
	var fc = $("#fc").val();//粉尘
	var fcfhcs = $("#fcfhcs").val();//粉尘防护措施有无
	var fcfhcsc = $("#fcfhcsc").val();//粉尘防护措施
	
	var fswz = $("#fswz").val();//放射物质
	var fswzfhcs = $("#fswzfhcs").val();//放射物质防护措施有无
	var fswzfhcsc = $("#fswzfhcsc").val();//放射物质防护措施
	
	var wlys = $("#wlys").val();//物理因素
	var wlysfhcs = $("#wlysfhcs").val();//物理因素防护措施有无
	var wlysfhcsc = $("#wlysfhcsc").val();//物理因素防护措施
	
	var hxwz = $("#hxwz").val();//化学物质
	var hxwzfhcs = $("#hxwzfhcs").val();//化学物质防护措施有无
	var hxwzfhcsc = $("#hxwzfhcsc").val();//化学物质防护措施
	
	var qtdw = $("#qtdw").val();//其他
	var qtdwfhcs = $("#qtdwfhcs").val();//其他防护措施有无
	var qtdwfhcsc = $("#qtdwfhcsc").val();//其他防护措施
	
	var wxyskz = $("input[name='wxyskz']:checked").val();//危险因素控制	
	var mbtz = $("#mbtz").val();//目标体重
	var wxyskzqt = $("#wxyskzqt").val();//危险因素控制其他		
		
	var sfys = $("#sfys").val();//随访医生
	var insertsql = "insert into ybrq(id,personid,name,sex,happentime,visityType,tw,ml,hxpl,gy,dy,height,weight,yaowei,bmi,ysxg,xyqk,rxyl,yjpl,"
						+"ryjl,sfjj,jjnl,zybjcs,gz,cysj,fc,fcfhcs,fcfhcsc,fswz,fswzfhcs,fswzfhcsc,wlys,wlysfhcs,wlysfhcsc,hxwz,hxwzfhcs,hxwzfhcsc,"
						+"qtdw,qtdwfhcs,qtdwfhcsc,wxyskz,mbtz,wxyskzqt,sfys) values("
					+"'"+Math.floor(Math.random()*10)+"',"
					+"'"+personid+"',"
					+"'"+name+"',"
					+"'"+sex+"',"
					+"'"+happentime+"',"
					+"'"+visityType+"',"
					+"'"+tw+"',"
					+"'"+ml+"',"
					+"'"+hxpl+"',"
					+"'"+gy+"',"
					+"'"+dy+"',"
					+"'"+height+"',"
					+"'"+weight+"',"
					+"'"+yaowei+"',"
					+"'"+bmi+"',"
					+"'"+ysxg+"',"
					+"'"+xyqk+"',"
					+"'"+rxyl+"',"
					+"'"+yjpl+"',"
					+"'"+ryjl+"',"
					+"'"+sfjj+"',"
					+"'"+jjnl+"',"
					+"'"+zybjcs+"',"
					+"'"+gz+"',"
					+"'"+cysj+"',"
					+"'"+fc+"',"
					+"'"+fcfhcs+"',"
					+"'"+fcfhcsc+"',"
					+"'"+fswz+"',"
					+"'"+fswzfhcs+"',"
					+"'"+fswzfhcsc+"',"
					+"'"+wlys+"',"
					+"'"+wlysfhcs+"',"
					+"'"+wlysfhcsc+"',"
					+"'"+hxwz+"',"
					+"'"+hxwzfhcs+"',"
					+"'"+hxwzfhcsc+"',"
					+"'"+qtdw+"',"
					+"'"+qtdwfhcs+"',"
					+"'"+qtdwfhcsc+"',"
					+"'"+wxyskz+"',"
					+"'"+mbtz+"',"
					+"'"+wxyskzqt+"'," 			 
					+"'"+sfys+"'"
					+")";
	app.InsertSql(insertsql);	 
	// transactionDB();//开启事务
	// executeSQL(insertsql);
	// transactionDBcommit();
	// closeDB();
	// selectSQL();
	
}

function selectNewData(){

	var selectsql = 'select * from ybrq where personid='+personid+" order by happentime desc";
		document.addEventListener('plusready', function(){
			plus.sqlite.selectSql({
				name: 'pad',
				sql: selectsql,
				success: function(data){
					console.log('selectSql success: ');
					// for(var i in data){
					// 	console.log(data[i].name); 
					// }
					if(null!=data&&data.length>0){
						var info = data[0];
						$("#xm").html(info.name);
						$("#xb").html(info.sex);
						$("#happentime").val(info.happentime);//随访日期
						$("input[name=sffs]:eq("+info.visityType+")").attr("checked",'checked');//随访方式
						$("#tw").val(info.tw);//体温
						$("#ml").val(info.ml);//脉率
						$("#hxpl").val(info.hxpl);//呼吸频率
						$("#gy").val(info.gy);//高压
						$("#dy").val(info.gy);//低压
						$("#height").val(info.height);//身高
						$("#weight").val(info.weight);//体重
						$("#yaowei").val(info.yaowei);//腰围
						$("#bmi").val(info.bmi);//bmi
						$("input[name=ysxg]:eq("+info.ysxg+")").attr("checked",'checked');//饮食习惯
						$("input[name=xyqk]:eq("+info.xyqk+")").attr("checked",'checked');//吸烟情况
						$("#rxyl").val(info.rxyl);//日吸烟量
						$("input[name=yjpl]:eq("+info.yjpl+")").attr("checked",'checked');//饮酒频率
						$("#ryjl").val(info.ryjl);//日饮酒量
						$("input[name=sfjj]:eq("+info.sfjj+")").attr("checked",'checked');//是否戒酒
						$("#jjnl").val(info.jjnl);//戒酒年龄
						$("input[name=zybjcs]:eq("+info.zybjcs+")").attr("checked",'checked');//职业病危害因素接触史
						$("#gz").val(info.gz);//工种
						$("#cysj").val(info.cysj);//从业时间
						$("#fc").val(info.fc);//粉尘
						$("#fcfhcs").val(info.fcfhcs);//粉尘防护措施有无
						$("#fcfhcsc").val(info.fcfhcsc);//粉尘防护措施
						
						$("#fswz").val(info.fswz);//放射物质
						$("#fswzfhcs").val(info.fswzfhcs);//放射物质防护措施有无
						$("#fswzfhcsc").val(info.fswzfhcsc);//放射物质防护措施
						
						$("#wlys").val(info.wlys);//物理因素
						$("#wlysfhcs").val(info.wlysfhcs);//物理因素防护措施有无
						$("#wlysfhcsc").val(info.wlysfhcsc);//物理因素防护措施
						
						$("#hxwz").val(info.hxwz);//化学物质
						$("#hxwzfhcs").val(info.hxwzfhcs);//化学物质防护措施有无
						$("#hxwzfhcsc").val(info.hxwzfhcsc);//化学物质防护措施
						
						$("#qtdw").val(info.qtdw);//其他
						$("#qtdwfhcs").val(info.qtdwfhcs);//其他防护措施有无
						$("#qtdwfhcsc").val(info.qtdwfhcsc);//其他防护措施
						
						$("input[name=wxyskz]:eq("+info.wxyskz+")").attr("checked",'checked');//危险因素控制	
						$("#mbtz").val(info.mbtz);//目标体重
						$("#wxyskzqt").val(info.wxyskzqt);//危险因素控制其他	
						$("#sfys").val(info.sfys);//随访医生
					}
					
					
				},
				fail: function(e){
					console.log('selectSql failed: '+JSON.stringify(e));
				}
			});
		});

}